package dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import conexion.BDConexion;
import dao.interfaces.ProgramaDAO;
import beans.Curso;
import beans.DetallePrograma;
import beans.Programa;

public class MySqlProgramaDAO implements ProgramaDAO {

	@Override
	public List<Programa> ListarPrograma() {
		List<Programa> lista = new ArrayList<Programa>();
		Connection cn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			cn = new BDConexion().obtenerConexion();
			String sql = "SELECT * FROM bd_inictel_dcc.programa;";
			pst = cn.prepareStatement(sql);
			rs = pst.executeQuery();
			while (rs.next()) {
				Programa p = new Programa(rs.getString(1), rs.getInt(2),
						rs.getString(3));
				lista.add(p);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				cn.close();
				pst.close();
				rs.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return lista;
	}

	@Override
	public List<Curso> ListarCursoxPrograma(String codpro) {
		List<Curso> lista = new ArrayList<Curso>();
		Connection cn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			cn = new BDConexion().obtenerConexion();
			String sql = "CALL bd_inictel_dcc.ListaCursoxProg(?);";
			pst = cn.prepareStatement(sql);
			pst.setString(1, codpro);
			rs = pst.executeQuery();
			while (rs.next()) {
				Curso c = new Curso();
				c.setCodigo(rs.getString(1));
				c.setNombre(rs.getString(2));
				lista.add(c);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				cn.close();
				pst.close();
				rs.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return lista;
	}

	@Override
	public List<DetallePrograma> ListarDetalle(String pro, String cur) {
		List<DetallePrograma> lista = new ArrayList<DetallePrograma>();
		Connection cn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			cn = new BDConexion().obtenerConexion();
			String sql = "SELECT * FROM bd_inictel_dcc.detalle_programa where cod_pro = ? and cod_cur = ?;";
			pst = cn.prepareStatement(sql);
			pst.setString(1, pro);
			pst.setString(2, cur);
			rs = pst.executeQuery();
			while (rs.next()) {
				DetallePrograma dp = new DetallePrograma(rs.getString(1),
						rs.getString(2), rs.getString(3), rs.getString(4),
						rs.getString(5), rs.getString(6), rs.getString(7),
						rs.getString(8));
				lista.add(dp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				cn.close();
				pst.close();
				rs.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return lista;
	}

	@Override
	public int agregarCurso(String codpro, String codcur) {
		int salida = -1;
		Connection cn = null;
		PreparedStatement pst = null;
		try {
			cn = new BDConexion().obtenerConexion();
			String sql = "INSERT INTO bd_inictel_dcc.detalle_programa (cod_pro,cod_cur) VALUES (?,?);";
			pst = cn.prepareStatement(sql);
			pst.setString(1, codpro);
			pst.setString(2, codcur);
			salida = pst.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				cn.close();
				pst.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return salida;
	}

	@Override
	public int grabarDetalle(DetallePrograma d) {
		int salida = -1;
		Connection cn = null;
		PreparedStatement pst = null;
		try {
			cn = new BDConexion().obtenerConexion();
			String sql = "UPDATE bd_inictel_dcc.detalle_programa SET cod_amb =? , fec_ini =? , fec_fin =? , hora_ini = ?, hora_fin =?, frecuencia =?  WHERE cod_pro =?  AND cod_cur = ?;";
			pst = cn.prepareStatement(sql);
			pst.setString(1, d.getAmbiente());
			pst.setString(2, d.getFecini());
			pst.setString(3, d.getFecfin());
			pst.setString(4, d.getHini());
			pst.setString(5, d.getHfin());
			pst.setString(6, d.getFrecuencia());
			pst.setString(7, d.getPrograma());
			pst.setString(8, d.getCurso());
			salida = pst.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				cn.close();
				pst.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return salida;
	}

	@Override
	public int borrarCursoPrograma(String pro, String cur) {
		int salida = -1;
		Connection cn = null;
		PreparedStatement pst = null;
		try {
			cn = new BDConexion().obtenerConexion();
			String sql = "DELETE FROM bd_inictel_dcc.detalle_programa WHERE cod_pro = ? and cod_cur = ?;";
			pst = cn.prepareStatement(sql);
			pst.setString(1, pro);
			pst.setString(2, cur);
			salida = pst.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				cn.close();
				pst.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return salida;
	}

}
